package com.xiaomei.manager.web.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaomei.manager.dao.InviteMapper;
import com.xiaomei.manager.model.Invite;
import com.xiaomei.manager.utils.DataValidate;
import com.xiaomei.manager.utils.GetNetworkTime;
import com.xiaomei.manager.utils.ReturnCode;
import com.xiaomei.manager.utils.ValidCodeUtils;
import com.xiaomei.manager.web.sms.HttpSender;

@Controller
public class InviteController {
	
	@Autowired
	private InviteMapper inviteMapper;
	
	@RequestMapping(value = "invite", method = RequestMethod.POST)
	@ResponseBody
	public String invite(HttpServletRequest request, HttpServletResponse response, Invite invite) {
		response.addHeader("Access-Control-Allow-Origin", "*");
		//判断mobile和code的值是否为空		
		if(invite.getCode() == null || invite.getMobile() == null || !DataValidate.IsMobilephone(invite.getMobile())){
			return ReturnCode.CODE_10004;
		}
		
		// 校验验证码是否正确
		if(!verifyValidcode(invite.getMobile(), invite.getCode(), request)) {
			return ReturnCode.CODE_10006;
		}
		
		int result = inviteMapper.insertSelective(invite);
		if(result == 1) {
			return ReturnCode.CODE_0;
		} 
		
		return ReturnCode.CODE_1;
	}
	
	/**
	 * 获取验证码
	 * @param mobile
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="validcode",method = RequestMethod.POST)
	@ResponseBody 
	public String GetValidCode(String mobile,HttpServletRequest request, HttpServletResponse response) {
		response.addHeader("Access-Control-Allow-Origin", "*");
		if(!DataValidate.IsMobilephone(mobile)) {
			return ReturnCode.CODE_10004;
		}
		
		//获取6位随机数作为验证码
		String code = ValidCodeUtils.getRandNum(6);
		String context = "小美出行为您提供的验证码为:" + code;
		if(!HttpSender.sendValidCode(mobile, context)) {
			return ReturnCode.CODE_10005;
		}
		
		request.getSession().getServletContext().setAttribute(mobile + "code", code);
		Long CurrentTime = GetNetworkTime.getNTSCDatetime().getTime();
		request.getSession().getServletContext().setAttribute(mobile + "codetime", CurrentTime);
		
		return ReturnCode.CODE_0;
	}
	
	/**
	 * 校验验证码是否正确
	 * @param code
	 * @param request
	 * @return
	 */
	private boolean verifyValidcode(String mobile, String code, HttpServletRequest request) {
		//取出session中保存的验证码获取时间
		Long last_time = (Long)request.getSession().getServletContext().getAttribute(mobile + "codetime");
//		Long last_time = (Long)request.getSession().getAttribute("codetime");
		if(last_time == null){
			return false;
		}
		
		//系统当前时间与验证码获取时间进行比较，查看验证码是否超过30s
		Long current_time = GetNetworkTime.getNTSCDatetime().getTime();
		if(!ValidCodeUtils.ValidTimeCheck(current_time, last_time)){
			return false;
		}
		//获取session中的验证码，比较客户端提交的验证码和生成的验证码
		String sys_code = (String)request.getSession().getServletContext().getAttribute(mobile + "code");
		if(!sys_code.equals(code)){
			return false;
		}
		request.getSession().getServletContext().removeAttribute(mobile + "code");
		return true;
	}
}
